package com.zxc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    /*@RequestMapping(value = "/login")
    public String login(HttpServletRequest request){
        //如果登录错误会返回两个异常UnknownAccountException（用户不存在）
        // IncorrectCredentialsException密码错误
        String msg = (String) request.getAttribute("shiroLoginFailure");
        if (!StringUtils.isEmpty(msg)) {
            if(msg.endsWith("UnknownAccountException")){
                request.getSession().setAttribute("msg","用户名不存在！");
            }
            if(msg.endsWith("IncorrectCredentialsException")){
                request.getSession().setAttribute("msg","密码错误！");
            }
        }
        return "/login";
    }*/

    @RequestMapping(value = "/login")
    public ModelAndView login(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        //如果登录错误会返回两个异常UnknownAccountException（用户不存在）
        //IncorrectCredentialsException密码错误
        String msg = (String) request.getAttribute("shiroLoginFailure");
        if (!StringUtils.isEmpty(msg)) {
            if(msg.endsWith("UnknownAccountException")){
                System.out.println("用户名不存在");
                mv.addObject("msg","用户不存在");
            }
            if(msg.endsWith("IncorrectCredentialsException")){
                System.out.println("密码错误");
                mv.addObject("msg","密码错误");
            }
        }
        mv.setViewName("login");
        return mv;
    }


}
